package com.qijian.quality.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qijian.quality.domain.FlowableResult;
import com.qijian.quality.mapper.FlowableResultMapper;
import com.qijian.quality.service.IFlowableResultService;
import com.qijian.tool.domain.Storage;
import com.qijian.tool.service.IStorageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 管理流程处理意见Service业务层处理
 *
 * @author qijian
 * @date 2022-04-06
 */
@Service
@RequiredArgsConstructor
public class FlowableResultServiceImpl extends ServiceImpl<FlowableResultMapper, FlowableResult> implements IFlowableResultService {

    private final FlowableResultMapper flowableResultMapper;
    private final IStorageService storageService;

    @Override
    public List<FlowableResult> listVo(QueryWrapper<FlowableResult> queryWrapper) {
        return flowableResultMapper.selectVo(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveResult(FlowableResult entity) {
        // 附件
        this.save(entity);
        List<Storage> files = entity.getFiles();
        if (CollectionUtil.isNotEmpty(files)) {
            files.forEach(action -> {
                action.setClassTable(FlowableResult.class.getSimpleName());
                action.setClassId(entity.getId());
            });
            storageService.updateBatchById(files);
        }
        return true;
    }

}
